import hashlib
import re

import scrapy
from ..items import MyFileItem
import time


class lawSpider(scrapy.Spider):
    name = 'khm_mojgov_law'
    # 柬埔寨司法部网站地址
    start_urls = ['http://www.moj.gov.kh/kh/law-regular']
    id = 0

    def parse(self, response):
        # 获得成员列表
        trs = response.xpath('//*[@id="dtLawDocument"]/tbody/tr')
        for tr in trs:
            # 法律名称
            legalNameS = tr.xpath('./td[2]//text()')
            legalName = ''
            if len(legalNameS):
                for legalNamel in legalNameS:
                    legalName = legalName + ' ' + legalNamel.get().strip()
                legalName = re.sub(r'''^ *''', r'''''', legalName)  # 去除开头的空格
                legalName = re.sub(r'''"''', r"'", legalName)   # 将双引号变为单引号
            # 发布时间
            publishDateS = tr.xpath('./td[4]//text()')
            publishDate = ''
            if len(publishDateS):
                for publishDatel in publishDateS:
                    publishDate = publishDate + ' ' + publishDatel.get().strip()
                publishDate = re.sub(r'''^ *''', r'''''', publishDate)  # 去除开头的空格
            # 下载地址
            downloadUrlS = tr.xpath('./td[5]/a/@href')
            downloadUrl = ''
            if len(downloadUrlS):
                downloadUrl = downloadUrlS.get().strip()

            if len(downloadUrl):
                item = MyFileItem()

                # 下载文件名
                fina = 'f' + str(hashlib.md5(downloadUrl.encode('utf-8')).hexdigest())
                # 唯一ID
                systemid = str(hashlib.md5(downloadUrl.encode('utf-8')).hexdigest())

                item['file_urls'] = [downloadUrl]
                item['country'] = 'Cambodia'
                item['website'] = 'mojgov'
                item['modular'] = 'law'
                item['title'] = ''
                item['ext'] = 'pdf'
                item['fina'] = fina
                item['chapNo'] = ''
                item['detailUrl'] = ''
                item['downloadUrl'] = ''
                item['htmls'] = ''
                item['htmlUrl'] = ''
                item['abstract'] = ''
                item['abstractUrl'] = ''

                item['LegalName'] = legalName
                item['Organizaation'] = ''
                item['PublishDate'] = publishDate
                item['EffectiveDate'] = ''
                item['SortA'] = 'LAWCOUNTRYJPZ'
                item['SortB'] = 'LANGUAGEGMY'
                item['SortC'] = ''
                item['SortD'] = ''
                item['SORTE'] = ''
                item['SORTF'] = ''
                item['Keyword'] = ''
                item['SORTG'] = ''
                item['ChapNo'] = ''
                item['Articles'] = ''
                item['Chapter'] = ''
                item['Section'] = ''
                item['SYS_FLD_DIGITFILENAME'] = fina
                item['FileUrl'] = ''
                item['DownLoadUrl'] = downloadUrl
                item['DownLoadWebNameC'] = '柬埔寨王国司法部网站'
                item['DownLoadWebNameE'] = 'Ministry of Justice,Kingdom of Cambodia'
                item['SYSID'] = systemid
                item['Website'] = 'Mojgov Law-regular'
                item['Isconversion'] = '0'
                item['Revisionmark'] = ''

                yield item

        # 翻页
        for nexta in response.css('#top-section div.container-fluid div.table-responsive.px-3 li.page-item.next > a'):
            yield response.follow(nexta, callback=self.parse, dont_filter=True)
            continue
